package com.ruihang.generalibrary.ui.dialog;


import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.R;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;

/**
 * A simple {@link Fragment} subclass.
 */
public class AlertDialogFragment extends BaseDialogFragment {

    private int modeTag;

    protected CustomFontTextView title;
    protected CustomFontTextView msg;
    protected CustomFontTextView cancle;
    protected CustomFontTextView ok;

    public static final int VIEW_ITEM_CANCLE = 11;
    public static final int VIEW_ITEM_OK = 12;

    @LayoutRes
    private int layoutRes = R.layout.fragment_alert_dialog;
    @StringRes
    private int cancleStrRes = -1;
    @StringRes
    private int okStrRes = -1;
    private int msgColor = -1;
    private float msgSize = -1;

    private String cancleStr;
    private String okStr;

    private String titleStr;
    private String msgStr;

    private boolean useStr;

    private Integer msgGravityTemp;

    private OnViewClick onViewClick;
    /**
     * 是否正在显示
     */
    private boolean isShowing;

    private boolean cancelOut;

    protected int in1;

    private OnViewInit onViewInit;

    public AlertDialogFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getContentRes() {
        return layoutRes;
    }

    @Override
    protected void initViews(Dialog dialog, Bundle savedInstanceState) {
        title = (CustomFontTextView) dialog.findViewById(R.id.alert_title);
        msg = (CustomFontTextView) dialog.findViewById(R.id.alert_msg);
        if (msgColor >= 0) {
            msg.setTextColor(msgColor);
        }
        if (msgSize >= 0) {
            msg.setTextSize(msgSize);
        }
        cancle = (CustomFontTextView) dialog.findViewById(R.id.alert_cancle);
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onViewClick == null || onViewClick.onViewClick(view, VIEW_ITEM_CANCLE, modeTag)) {
                    dismiss();
                }
            }
        });
        ok = (CustomFontTextView) dialog.findViewById(R.id.alert_ok);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onViewClick == null || onViewClick.onViewClick(view, VIEW_ITEM_OK, modeTag)) {
                    dismiss();
                }
//                dismiss();
            }
        });
        dialog.setCanceledOnTouchOutside(cancelOut);
        if (msgGravityTemp != null) {
            msg.setGravity(msgGravityTemp);
            msgGravityTemp = null;
        }
        if (onViewInit != null) {
            onViewInit.onViewInit(dialog, savedInstanceState);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        JLog.e("isVisibleToUser=" + isVisibleToUser);
        if (isVisibleToUser && msg != null) {

        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        JLog.e("hidden=" + hidden);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isShowing) {
            isShowing = true;
            setText(cancle, cancleStrRes, cancleStr);
            setText(ok, okStrRes, okStr);
            if (TextUtils.isEmpty(titleStr)) {
                title.setVisibility(View.GONE);
            } else {
                title.setVisibility(View.VISIBLE);
                title.setText(titleStr);
            }
            msg.setText(msgStr);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
//        JLog.e("onPause");
        isShowing = false;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        JLog.e("call interface=" + dialog);
        if (callBack != null) {
            Message msg = Message.obtain();
            msg.what = WHAT_DISMISS;
            callBack.onCallBackMsg(this, msg);
        }
        super.onDismiss(dialog);
    }

    public AlertDialogFragment setIn1(int in1) {
        this.in1 = in1;
        return this;
    }

    public int getIn1() {
        return in1;
    }

    public AlertDialogFragment setLayoutRes(int layoutRes) {
        this.layoutRes = layoutRes;
        return this;
    }

    public AlertDialogFragment setMsgGravity(int gravity) {
        if (msg != null) {
            msg.setGravity(gravity);
        } else {
            msgGravityTemp = gravity;
        }
        return this;
    }

    public AlertDialogFragment setMsgColor(int msgColor) {
        this.msgColor = msgColor;
        return this;
    }

    public AlertDialogFragment setMsgSize(float msgSize) {
        this.msgSize = msgSize;
        return this;
    }

    public AlertDialogFragment setCancelOut(boolean cancelOut) {
        this.cancelOut = cancelOut;
        if (msg != null) {
            getDialog().setCanceledOnTouchOutside(cancelOut);
        }
        return this;
    }

    public AlertDialogFragment setCallBackS(ICallBack callBackS) {
        setCallBack(callBackS);
        return this;
    }

    private boolean setText(CustomFontTextView tv, @StringRes int strRes, String tex) {
        if (strRes == -1 || useStr) {
            tv.setText(tex);
            return !TextUtils.isEmpty(tex);
        } else {
            tv.setText(strRes);
            return true;
        }
    }

    public AlertDialogFragment setCancleStrRes(int cancleStrRes) {
        this.cancleStrRes = cancleStrRes;
        return this;
    }

    public AlertDialogFragment setOkStrRes(int okStrRes) {
        this.okStrRes = okStrRes;
        return this;
    }

    public AlertDialogFragment setCancleStr(String cancleStr) {
        this.cancleStr = cancleStr;
        if (this.cancleStr == null) {
            this.cancleStr = "";
        }
        return this;
    }

    public AlertDialogFragment setOkStr(String okStr) {
        this.okStr = okStr;
        if (this.okStr == null) {
            this.okStr = "";
        }
        return this;
    }

    public AlertDialogFragment setTitleStr(String titleStr) {
        this.titleStr = titleStr;
        if (this.titleStr == null) {
            this.titleStr = "";
        }
        if (isShowing) {
            title.setText(this.titleStr);
            if (TextUtils.isEmpty(this.titleStr)) {
                title.setVisibility(View.GONE);
            } else if (title.getVisibility() == View.GONE) {
                title.setVisibility(View.VISIBLE);
            }
        }
        return this;
    }

    public AlertDialogFragment setMsgStr(String msgStr) {
        this.msgStr = msgStr;
        if (this.msgStr == null) {
            this.msgStr = "";
        }
        if (isShowing) {
            msg.setText(this.msgStr);
        }
        return this;
    }

    public AlertDialogFragment setUseStr(boolean useStr) {
        this.useStr = useStr;
        return this;
    }

    public AlertDialogFragment setOnViewClick(OnViewClick onViewClick) {
        this.onViewClick = onViewClick;
        return this;
    }

    public int getModeTag() {
        return modeTag;
    }

    public void setModeTag(int modeTag) {
        this.modeTag = modeTag;
    }

    public CustomFontTextView getTitleView() {
        return title;
    }

    public CustomFontTextView getMsgView() {
        return msg;
    }

    public CustomFontTextView getCancleView() {
        return cancle;
    }

    public CustomFontTextView getOkView() {
        return ok;
    }

    public AlertDialogFragment setOnViewInit(OnViewInit onViewInit) {
        this.onViewInit = onViewInit;
        return this;
    }

    public interface OnViewClick {
        /**
         * @param view
         * @param viewItem
         * @param mode
         * @return 是否dismiss
         */
        boolean onViewClick(View view, int viewItem, int mode);
    }

    public interface OnViewInit {
        void onViewInit(Dialog dialog, Bundle savedInstanceState);
    }
}
